package com.hrm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hrm.dto.EmpSalaryDTO;
import com.hrm.dto.EmpSalaryRecordDTO;
import com.hrm.entity.EmpSalary;
import com.hrm.entity.Employee;
import com.hrm.entity.MyPage;
import com.hrm.service.EmpSalaryService;
import com.hrm.utils.JsonMsg;
import com.hrm.utils.Layui;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther: 朱晓龙
 * @Date: 2020/6/1 21:09
 * @Description: 员工薪酬管理控制器
 */
@Controller
@RequestMapping("/salary")
public class EmpSalaryController {

    @Autowired
    private EmpSalaryService salaryService;

    /**
     * 跳转到薪酬管理界面
     *
     * @return String
     */
    @RequestMapping("/salaryPage.html")
    public String salaryPage() {
        return "salaryManagement/salaryPage";
    }


    @RequestMapping("/empSalaryList.html")
    @ResponseBody
    public ResponseEntity<Layui> salaryList(MyPage myPage) {
        PageHelper.startPage(myPage.getPageNum(), myPage.getPageSize());
        List<EmpSalaryDTO> empSalaryDTOList = salaryService.empSalaryList();
        PageInfo pageInfo = new PageInfo(empSalaryDTOList, 5);
        Layui layui = Layui.data(pageInfo.getTotal(), pageInfo.getList());
        return ResponseEntity.ok(layui);
    }

    /**
     * 弹出薪资基本信息面板
     *
     * @return String
     */
    @RequestMapping("/salaryInfo.html")
    public String salaryInfo() {
        return "salaryManagement/salaryInfo";
    }


    /**
     * 弹出基本工资面板
     *
     * @return String
     */
    @RequestMapping("/basicSalaryInfo.html")
    public String basicSalaryInfo() {
        return "salaryManagement/basicSalaryInfo";
    }


    /**
     * 弹出奖惩信息面板
     *
     * @return String
     */
    @RequestMapping("/awardAndPunishInfo.html")
    public String awardAndPunishInfo() {
        return "salaryManagement/awardAndPunishInfo";
    }



    @RequestMapping("/updateSalaryPayTimeInfo.html")
    @ResponseBody
    public JsonMsg updateSalaryPayTimeInfo(Integer salaryId, Integer payTime) {
        boolean isSuccess = salaryService.updateSalaryPayTimeInfo(salaryId, payTime);
        return isSuccess ? JsonMsg.success() : JsonMsg.fail();
    }

    @RequestMapping("/updateBasicSalary.html")
    @ResponseBody
    public JsonMsg updateBasicSalary(Integer id, Integer basicSalary) {
        boolean isSuccess = salaryService.updateBasicSalary(id, basicSalary);
        return isSuccess ? JsonMsg.success() : JsonMsg.fail();
    }

    /**
     * 删除一条奖惩记录
     *
     * @return JsonMsg
     */
    @RequestMapping("/delSalaryRecord.html")
    @ResponseBody
    public JsonMsg delSalaryRecord(EmpSalaryRecordDTO recordDTO){
        boolean isSuccess = salaryService.delSalaryRecord(recordDTO);
        return isSuccess ? JsonMsg.success() : JsonMsg.fail();
    }

    /**
     * 获取员工的所有奖惩记录
     *
     * @return String
     */
    @RequestMapping("/records.html")
    public ResponseEntity<Layui> records(MyPage myPage, Integer id) {
        PageHelper.startPage(myPage.getPageNum(), myPage.getPageSize());
        List<EmpSalaryRecordDTO> recordDTOList = salaryService.recordList(id);

        PageInfo pageInfo = new PageInfo(recordDTOList,5);
        Layui layui = Layui.data(pageInfo.getTotal(),pageInfo.getList());

        return ResponseEntity.ok(layui);
    }


    @RequestMapping("/addRecord.html")
    @ResponseBody
    public JsonMsg addRecord(EmpSalaryRecordDTO recordDTO){
        boolean isSuccess = salaryService.addRecord(recordDTO);
        return isSuccess ? JsonMsg.success() : JsonMsg.fail();
    }

}
